package com.ruoyi.project.ledger.controller;

import java.util.List;

import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.framework.security.service.PermissionService;
import com.ruoyi.project.system.domain.SysDept;
import com.ruoyi.project.system.domain.SysUser;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.project.ledger.domain.LedgerForkliftInspect;
import com.ruoyi.project.ledger.service.ILedgerForkliftInspectService;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.web.page.TableDataInfo;

/**
 * 叉车检查项目Controller
 *
 * @author ruoyi
 * @date 2020-04-28
 */
@RestController
@RequestMapping("/ledger/inspect")
public class LedgerForkliftInspectController extends BaseController
{
    @Autowired
    private ILedgerForkliftInspectService ledgerForkliftInspectService;
    @Autowired
    private PermissionService permissionService;

    /**
     * 查询叉车检查项目列表
     */
//    @PreAuthorize("@ss.hasPermi('ledger:inspect:list')")
    @GetMapping("/list")
    public TableDataInfo list(LedgerForkliftInspect ledgerForkliftInspect)
    {
        SysUser user = SecurityUtils.getLoginUser().getUser();
        // 场站id
        Long stationId = user.getStationId();
        if(stationId != null){
            ledgerForkliftInspect.setStationId(stationId);
        }
        // 管辖区域
        List<String> areaList = user.getAreaList();
        String areas = String.join(",", areaList);
        String managementCharacters = user.getManagementCharacters();
        ledgerForkliftInspect.setAreas(areas);
        ledgerForkliftInspect.setManagementCharacters(managementCharacters);
        startPage();
        List<LedgerForkliftInspect> list = ledgerForkliftInspectService.selectLedgerForkliftInspectList(ledgerForkliftInspect);
        return getDataTable(list);
    }

    /**
     * 导出叉车检查项目列表
     */
    @PreAuthorize("@ss.hasPermi('ledger:inspect:export')")
    @Log(title = "叉车检查项目", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(LedgerForkliftInspect ledgerForkliftInspect)
    {
        SysUser user = SecurityUtils.getLoginUser().getUser();
        // 场站id
        Long stationId = user.getStationId();
        if(stationId != null){
            ledgerForkliftInspect.setStationId(stationId);
        }
        // 管辖区域
        List<String> areaList = user.getAreaList();
        String areas = String.join(",", areaList);
        String managementCharacters = user.getManagementCharacters();
        ledgerForkliftInspect.setAreas(areas);
        ledgerForkliftInspect.setManagementCharacters(managementCharacters);
        List<LedgerForkliftInspect> list = ledgerForkliftInspectService.selectLedgerForkliftInspectList(ledgerForkliftInspect);
        ExcelUtil<LedgerForkliftInspect> util = new ExcelUtil<LedgerForkliftInspect>(LedgerForkliftInspect.class);
        return util.exportExcel(list, "inspect");
    }

    /**
     * 获取叉车检查项目详细信息
     */
//    @PreAuthorize("@ss.hasPermi('ledger:inspect:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(ledgerForkliftInspectService.selectLedgerForkliftInspectById(id));
    }

    /**
     * 新增叉车检查项目
     */
    @PreAuthorize("@ss.hasPermi('ledger:inspect:add')")
    @Log(title = "叉车检查项目", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody LedgerForkliftInspect ledgerForkliftInspect)
    {
        // 判断是否是场站角色
        if(!permissionService.hasRole("admin") && (permissionService.hasRole("station") || permissionService.hasRole("hs_station"))){
            SysUser user = SecurityUtils.getLoginUser().getUser();
            Long stationId = user.getStationId();
            ledgerForkliftInspect.setStationId(stationId);
            ledgerForkliftInspect.setCreateId(String.valueOf(user.getUserId()));
        } else{
            return AjaxResult.error("当前角色无新增权限");
        }
        return toAjax(ledgerForkliftInspectService.insertLedgerForkliftInspect(ledgerForkliftInspect));
    }

    /**
     * 修改叉车检查项目
     */
    @PreAuthorize("@ss.hasPermi('ledger:inspect:edit')")
    @Log(title = "叉车检查项目", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody LedgerForkliftInspect ledgerForkliftInspect)
    {
        // 判断是否是场站角色
        if(!permissionService.hasRole("admin") && (permissionService.hasRole("station") || permissionService.hasRole("hs_station"))){
            SysUser user = SecurityUtils.getLoginUser().getUser();
            Long stationId = user.getStationId();
            ledgerForkliftInspect.setStationId(stationId);
            ledgerForkliftInspect.setCreateId(String.valueOf(user.getUserId()));
        } else{
            return AjaxResult.error("当前角色无修改权限");
        }
        return toAjax(ledgerForkliftInspectService.updateLedgerForkliftInspect(ledgerForkliftInspect));
    }

    /**
     * 删除叉车检查项目
     */
    @PreAuthorize("@ss.hasPermi('ledger:inspect:remove')")
    @Log(title = "叉车检查项目", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(ledgerForkliftInspectService.deleteLedgerForkliftInspectByIds(ids));
    }
}
